package org.xiangfei.client.handler;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import org.xiangfei.client.XFCallback;
import org.xiangfei.codec.XFMessageDecoder;
import org.xiangfei.codec.XFMessageEncoder;

public class MessageClientInitializer extends ChannelInitializer<Channel> {
    private XFCallback callback;
    public MessageClientInitializer(XFCallback callback) {
        this.callback = callback;
    }
    @Override
    protected void initChannel(Channel ch) throws Exception {
        System.out.println(callback + "+=" );
        ChannelPipeline pipeline = ch.pipeline();
//        pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));  
//        pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));  
        pipeline.addLast("decoder", new XFMessageDecoder());  
        pipeline.addLast("encoder", new XFMessageEncoder()); 

        // frameencoder encoder 区别
        //ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
        //ch.pipeline().addLast(new ProtobufDecoder(Auth.AuthResponse.getDefaultInstance()));
        //encoded
        //ch.pipeline().addLast(new LengthFieldPrepender(4));
        // 注册handler
        pipeline.addLast(new MessageClientHandler(callback));
    }
}